<!--
 * @Author: 郝奔 1425466221@qq.com
 * @Date: 2024-11-22 09:21:08
 * @LastEditors: 郝奔 1425466221@qq.com
 * @LastEditTime: 2024-12-26 13:26:41
 * @FilePath: /HG-dqbasic_open_platform/src/framework/components/Breadcrumb/index.vue
 * @Description: 
 * Copyright (c) 2024-present HBIS Digital Technology Co.,Ltd. All rights reserved. 
-->
<template>
  <el-breadcrumb separator-class="el-icon-arrow-right" class="h-[50px] flex items-center">
    <transition-group name="breadcrumb">
      <el-breadcrumb-item v-for="(item, index) in breadcrumbs" :key="item.path">
        <span class="no-redirect"
          :style="{ color: item.redirect === 'noRedirect' || index == breadcrumbs.length - 1 ? TopActiveTextColor : TopTextColor }">
          {{ generateTitle(item.meta.title as string) }}
        </span>
      </el-breadcrumb-item>
    </transition-group>
  </el-breadcrumb>
</template>

<script setup lang="ts">
import { onBeforeMount, ref, watch } from 'vue';
import { RouteLocationMatched } from 'vue-router';
import { useRoute } from 'vue-router';
// import { compile } from 'path-to-regexp';
// import router from '@/router';
import { generateTitle } from '@/framework/utils/i18n';
import { useSettingsStore } from '@/framework/store/modules/settings';
const currentRoute = useRoute();
const settingsStore = useSettingsStore();
// const pathCompile = (path: string) => {
//   const { params } = currentRoute;
//   const toPath = compile(path);
//   return toPath(params);
// };

const breadcrumbs = ref([] as Array<RouteLocationMatched>);
function getBreadcrumb(): void {
  const matched = currentRoute.matched.filter(
    item => item.meta && item.meta.title
  );
  // const first = matched[0];
  // if (!isDashboard(first)) {
  //   matched = [
  //     { path: '/dashboard', meta: { title: 'dashboard' } } as any
  //   ].concat(matched);
  // }
  const matchList = matched.filter(item => {
    return item.meta && item.meta.title && item.meta.breadcrumb !== false;
  });
  if (matchList[0].meta.parentTitle && Object.keys(matchList[0].meta.parentTitle).length > 0) {
    breadcrumbs.value = [
      matchList[0].meta.parentTitle as any
    ].concat(matchList);
  } else {
    breadcrumbs.value = []
  }
}

// function isDashboard(route: RouteLocationMatched) {
//   const name = route && route.name;
//   if (!name) {
//     return false;
//   }
//   return (
//     name.toString().trim().toLocaleLowerCase() ===
//     'Dashboard'.toLocaleLowerCase()
//   );
// }

// function handleLink(item: any) {
//   const { redirect, path } = item;
//   if (redirect) {
//     router.push(redirect).catch(err => {
//       console.warn(err);
//     });
//     return;
//   }
//   router.push(pathCompile(path)).catch(err => {
//     console.warn(err);
//   });
// }
const TopTextColor = computed(() => settingsStore.TopTextColor);
const TopActiveTextColor = computed(() => settingsStore.TopActiveTextColor);
watch(
  () => currentRoute.path,
  path => {
    if (path.startsWith('/redirect/')) {
      return;
    }
    getBreadcrumb();
  }
);

onBeforeMount(() => {
  getBreadcrumb();
});
</script>

<style lang="scss" scoped>
.app-breadcrumb.el-breadcrumb {
  display: inline-block;
  font-size: 14px;
  line-height: 50px;
  margin-left: 8px;

  .no-redirect {
    color: #97a8be;
    cursor: text;
  }
}
</style>
